package com.chinaisafe.user.service.impl;

import com.chinaisafe.user.entity.SysUser;
import com.chinaisafe.user.mapper.SysUserMapper;
import com.chinaisafe.user.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaisafe.user.vo.request.SysUserReqVO;
import com.chinaisafe.user.vo.response.SysUserRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.base.mybatisplus.constant.MybatisplusConstants;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaisafe.base.mybatisplus.util.MpQueryWrapperPageUtil;

import java.util.Arrays;
import java.util.Optional;

/**
 * SysUserServiceImpl
 * Description：用户服务实现类
 *
 * @author xuefu.shu
 * @date 2024-07-12 09:55:54
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {



    @Override
    public IPage<SysUserRespVO> getPageList(SysUserReqVO sysUserReqVO) {
        // 分页设置
        Page<SysUser> page = MpQueryWrapperPageUtil.buildWrapperPage(new Page<>(), sysUserReqVO);
        // 查询条件构造
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getDeleted, MybatisplusConstants.LOGIC_NOT_DELETE_VALUE);
        return baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserRespVO add(SysUserReqVO sysUserReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysUserReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserRespVO update(SysUserReqVO sysUserReqVO) {
        // 新增和编辑逻辑相同的情况下 直接调用新增或编辑方法
        return this.addOrUpdate(sysUserReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserRespVO addOrUpdate(SysUserReqVO sysUserReqVO) {
        Long sysUserId = sysUserReqVO.getId();
        if (sysUserId != null) {
            // 编辑
            SysUser sysUser = baseMapper.selectById(sysUserId);
            Optional.ofNullable(sysUser).orElseThrow(() -> BizException.build("未找到要修改的用户信息"));
        }
        SysUser sysUser2Save = sysUserReqVO.convertToEntity();
        boolean success = this.saveOrUpdate(sysUser2Save);
        if (success) {
            return SysUserRespVO.convertFromEntity(sysUser2Save);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    @Override
    public SysUserRespVO getDetailById(Long sysUserId) {
        SysUser sysUser = baseMapper.selectById(sysUserId);
        Optional.ofNullable(sysUser).orElseThrow(() -> BizException.build("未找到对应用户信息"));
        return SysUserRespVO.convertFromEntity(sysUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] sysUserIds) {
        if (sysUserIds.length > 0) {
            this.removeByIds(Arrays.asList(sysUserIds));
        }
    }

}
